/**
 * Created by Administrator on 2018/3/22 0022.
 */

var getInfo = function () {
    $('#showPicture').html('');
    $.post($().AbsUrlParse("/web/image/queryImage"), null, function (res) {
        var data = res.res.data;
        var htmlInformation = "";
        if (data.length != 0) {
            for (var i = 0; i < data.length; i++) {
                htmlInformation += "<div class='col-md-2' value=" + data[i].id + ">" +
                    '<image src=' + File_Download_Url + data[i].image + ' style="width:100%;height:150px" onclick="enlargeImage(this)"/>' +
                    '<a href="javascript:void(0)" onclick="deletePic(' + data[i].id + ')" style="color: red">删除</a>' +
                    "</div>";
            }
            htmlInformation += "<div class='col-md-2'>" +
                '<img class="addImage" src=' + $().AbsUrlParse("/static/images/commons/img_add.png") + ' width="100%" height="150" id="photo" ' +
                '">' +
                "</div>";
            $('#showPicture').append(htmlInformation);
        } else {
            frame();
        }
    });
};
getInfo();

var frame = function () {
    $('#showPicture').append('');
    var htmlInformation = "";
    htmlInformation += "<div class='col-md-2'>" +
        '<img class="addImage" src=' + $().AbsUrlParse("/static/images/commons/img_add.png") + ' width="100%" height="150" id="photo" ' +
        'onclick="enlargeImage(this)">' +
        "</div>";
    $('#showPicture').append(htmlInformation);
};

bindClick("#addPic", function () {
    var pic = $("#photo").attr("img-path");
    if (pic == "") {
        hint("请选择图片");
        return;
    }
    $.post($().AbsUrlParse("/web/image/addImage"), {picture: pic}, function (data) {
        if (data.state == true) {
            if (data.res.code == 40000) {
                hint("上传成功");
                // window.location.reload();
                // $("#picture").val('');
                $("#photo").val('');
                $("#showPicture").load($().AbsUrlParse("/web/image/findImage") + "#showPicture");
                $('#showPicture').html('');
                // addLoop();
            } else {
                hint(data.res.msg);
            }
        } else {
            hint(data.msg);
        }
    })
});



var deletePic = function (id) {
    $.post($().AbsUrlParse("/web/image/deletePic"), {id: id}, function (data) {
        if (data.state == true) {
            if (data.res.code == 40000) {
                hint("删除成功");
                getInfo();
            } else {
                hint(data.res.msg);
            }
        } else {
            hint(data.msg);
        }
    })
};

var addLoop = function () {
    $.post($().AbsUrlParse("/web/image/queryImage"), null, function (res) {
        var data = res.res.data;
        if (data.length != 0) {
            var htmlInformation = "";
            htmlInformation += "<div class='col-md-2'>" +
                '<img class="addImage" src=' + $().AbsUrlParse("/static/images/commons/img_add.png") + ' width="100%" height="150" id="photo" onclick="enlargeImage(this)">' +
                "</div>";
            $('#showPicture').append(htmlInformation);
        }
    })
};

function enlargeImage(value) {
    showModel("detailImage-div", function () {
        var imgs = '<image src=' + value.src + ' style="height:500px" />';
        $("#image").html(imgs);
    })
}